import request from "@/utils/request";

// Add a script
export const addScriptService = (title, author, content, tags) => {
    const params = new URLSearchParams();
    params.append('title', title);
    params.append('author', author);
    params.append('content', content);
    params.append('tags', tags);
    return request.post('/scripts/add', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

// Get a script by ID
export const getScriptByIdService = (id) => {
    return request.get(`/scripts/${id}`);
}

// Get scripts by author
export const getScriptByAuthor = (author) => {
    return request.get(`/scripts/author/${author}`);
}

// Search scripts by title
export const searchScriptsByTitleService = (title) => {
    return request.get('/scripts/search', {
        params: {
            title: title
        }
    });
}
// 某用户可以修改自己上传的剧本
export const updateScriptsService = (id, title, content, tags) => {
    const params = new URLSearchParams();
    params.append('id', id);
    params.append('title', title);
    params.append('content', content);
    params.append('tags', tags);
    return request.put('/scripts/update', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

// Get recent played scripts by user ID
export const getRecentPlayedScriptsService = (userId) => {
    return request.get(`/scripts/recent/${userId}`);
}

// Add a scene to a script
export const addSceneService = (scriptId, sceneDescription) => {
    //console.log("添加场景ing")
    //console.log(scriptId,sceneDescription)
    const params = new URLSearchParams();
    params.append('scriptId', scriptId);
    params.append('description', sceneDescription);
    // console.log(params.get('scriptId'))
    // console.log(params.get('description'))
    return request.post('/scripts/scenes/add', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }).then(response => {
        console.log('Request successful', response);
    }).catch(error => {
        console.error('Request failed', error);
    });
}

// Get scene by ID
export const getSceneByIdService = (id) => {
    return request.get(`/scripts/scenes/${id}`);
}

// Get scenes by script ID
export const getScenesByScriptIdService = (scriptId) => {
    return request.get(`/scripts/scenes/script/${scriptId}`);
}

// Update a scene
export const updateSceneService = (id, sceneDescription) => {
    const params = new URLSearchParams();
    params.append('id', id);
    params.append('description', sceneDescription);
    return request.put('/scripts/scenes/update', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

// Delete a scene by ID
export const deleteSceneService = (id) => {
    return request.delete(`/scripts/scenes/delete/${id}`);
}

export const deleteScriptService = (id) => {
    return request.delete(`/scripts/delete/${id}`);
}

// Add a role to a script
export const addRoleService = (scriptId, roleName, roleDescription, traits) => {
    const params = new URLSearchParams();
    params.append('scriptId', scriptId);
    params.append('name', roleName);
    params.append('description', roleDescription);
    params.append('traits', traits);
    return request.post('/scripts/roles/add', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

// Get role by ID
export const getRoleByIdService = (id) => {
    return request.get(`/scripts/roles/${id}`);
}

// Get roles by script ID
export const getRolesByScriptIdService = (scriptId) => {
    return request.get(`/scripts/roles/script/${scriptId}`);
}

// Update a role
export const updateRoleService = (id, scriptId, name, description, traits) => {
    const params = new URLSearchParams();
    params.append('id', id);
    params.append('scriptId', scriptId)
    params.append('name', name);
    params.append('description', description);
    params.append('traits', traits)
    return request.put('/scripts/roles/update', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

// Get all scripts
export const getAllScriptsService = () => {
    return request.get('/scripts/all');
}

// Delete a role by ID
export const deleteRoleService = (id) => {
    return request.delete(`/scripts/roles/delete/${id}`);
}

export function uploadImageService(formData) {
    return request.post("/scripts/uploadImage", formData, {
        headers: {
            "Content-Type": "multipart/form-data"
        }
    });
}
